package com.henzil.siji.server;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;

import com.henzil.siji.bean.ResultBean;
import com.henzil.siji.bean.VehiclesInfo;

import android.util.Log;

public class QueryVehicleViolation {

	

	
	public List<ResultBean> queryResult(VehiclesInfo vehiclesInfo) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("hphm", vehiclesInfo.getCarLicense()));
		params.add(new BasicNameValuePair("fdjh", vehiclesInfo.getEngineModel()));
		params.add(new BasicNameValuePair("hpzl", "02"));
		params.add(new BasicNameValuePair("sf", "11"));

		return doPost(QueryUtil.BEIJING_URL, params);
	}

	private List<ResultBean> doPost(String url, List<NameValuePair> params) {

		List<ResultBean>  resultBeans = new ArrayList<ResultBean>();
		ResultBean resultBean;

		/* 建立HTTPPost对象 */
		HttpPost httpRequest = new HttpPost(url);

		String strResult = "doPostError";
		HttpClient httpClient = new DefaultHttpClient();
		try {
			/* 添加请求参数到请求对象 */
			httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			/* 发送请求并等待响应 */
			HttpResponse httpResponse = httpClient.execute(httpRequest);
			/* 若状态码为200 ok */
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				/* 读返回数据 */
				strResult = EntityUtils.toString(httpResponse.getEntity());

			} else {
				// TODO 抛出异常
				Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
			}

			Parser parser = new Parser(strResult);
			NodeList nodeList = parser.parse(new AndFilter(new NodeFilter[] {
					new TagNameFilter("tr"),
					new HasAttributeFilter("onMouseOver",
							"this.style.backgroundColor='ff9900'") }));

			for (int i = 0; i < nodeList.size(); i++) {

				Log.v("tag>>>>>", ">>>>>>>>>"
						+ nodeList.elementAt(i).getChildren().elementAt(3)
								.toPlainTextString()
						+ ">>>>>>>>>"
						+ nodeList.elementAt(i).getChildren().elementAt(5)
								.toPlainTextString()
						+ ">>>>>>>>>"
						+ nodeList.elementAt(i).getChildren().elementAt(7)
								.toPlainTextString()
						+ ">>>>>>>>>"
						+ nodeList.elementAt(i).getChildren().elementAt(9)
								.toPlainTextString());
				resultBean = new ResultBean();
				resultBean.setTime(nodeList.elementAt(i).getChildren()
						.elementAt(3).toPlainTextString());
				resultBean.setAddress(nodeList.elementAt(i).getChildren()
						.elementAt(5).toPlainTextString());
				resultBean.setReason(nodeList.elementAt(i).getChildren()
						.elementAt(7).toPlainTextString());
				resultBean.setStatus(nodeList.elementAt(i).getChildren()
						.elementAt(9).toPlainTextString());
				resultBeans.add(resultBean);
			}

		} catch (Exception e) {
			
			Log.e("tag", "query vehicle exception>>>>>>>>>" + e);
		}
		return resultBeans;
	}
}